// +----------------------------------------------------------------------
// | BossHongshi [鸿时互娱管理系统]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.hongshihuyu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wgh
// +----------------------------------------------------------------------
const fly=require("flyio")
module.exports = class extends think.cmswing.center {
  async __before() {
    await super.__before();
    // 判断是否登陆
    // await this.weblogin();
    if (!this.is_login) {
      // 判断浏览客户端
      if (this.isMobile) {
        // 手机端直接跳转到登录页面
        return this.redirect('/center/public/login');
      } else {
        return this.redirect('/cmswing/error/login');
      }
    }
  }
  
  // 我的代理
  async indexAction() {
    const proxy_id = this.para('proxy_id') || null;
    const opt = this.para('opt') || 0;
    const userInfo = await this.model('member').find(this.user.uid);
    let map;
    if (opt == 0) { // 我的代理
      map = {
        is_admin:0,
        status:1,
        ia_uname:userInfo.proxy_id
      };
    } else if (opt == 1) { // 推荐代理
    	
      map = {
    	status:1,
    	proxy_id:userInfo.ia_uname
      };
    }

    const data = await this.model('member').where(map).page(this.para('page')).order('reg_time DESC').countSelect();
    const html = this.pagination(data);
    this.assign('pagination', html);
    this.assign('count', data.count);
    
    for (const v of data.data) {
        //v.reg_time = this.moment(v.reg_time).format('lll');
        const profitlog = await this.model('profit_log').where({user_id:this.user.uid,act_id:v.id,status:1}).getField('amount');
		console.log("profitlog --:"+JSON.stringify(profitlog));
		v.profit = think.isEmpty(profitlog)?0:eval(profitlog.join('+'));
    }
    
    this.assign('list', data.data);
    this.assign('opt',opt);
    
    this.meta_title = '我的代理';
    // 判断浏览客户端
    if (this.isMobile) {
      if (this.isAjax('get')) {
        return this.json(data);
      } else {
        this.active = 'agent/index';
        return this.display(this.mtpl());
      }
    } else {
      return this.display();
    }
  }

//代理信息
  async infoAction() {
	
		const id = this.para('id') || null;
	    // 筛选
	    const agentInfo = await this.model('member').find(id);
	    this.assign('agentInfo', agentInfo);
	    console.log("id --:"+id);
	    //当前代理自己的收益
	    const selfprofitlog = await this.model('profit_log').where({user_id:id,status:1}).sum('amount');
	    
	    console.log("selfprofitlog -111111-:"+selfprofitlog);
		
		this.assign('selfprofitTotal', think.isEmpty(selfprofitlog)?0:selfprofitlog);
	    
		//当前代理下属代理的收益
		const memberIds = await this.model('member').where({
	        is_admin:0,
	        status:1,
	        ia_uname:agentInfo.proxy_id
	      }).getField('id');
		
		console.log("memberIds --:"+JSON.stringify(memberIds));
		
		var mbprofitlog =0;
		if(!think.isEmpty(memberIds)){
			mbprofitlog = await this.model('profit_log').where(
						{
							user_id:['IN',memberIds],
							status:1
						}).sum('amount');
		}
		
		console.log("mbprofitlog --:"+think.isEmpty(mbprofitlog)?0:mbprofitlog);
		
		this.assign('mbprofitTotal', think.isEmpty(mbprofitlog)?0:mbprofitlog);
	    //总收益
		this.assign('profitTotal',Number(selfprofitlog)+Number(mbprofitlog));

	    // 直属代理统计
	    const lowernum = await this.model('member').where({
	    	is_admin:0,
	    	ia_uname:agentInfo.proxy_id
	    }).count('id');
	    this.assign('loweranum', lowernum);
	    
	    const url='http://47.97.119.70:5166/GameSupport/dn?cmd=getPlayers&agentId='+agentInfo.proxy_id+'&sign=password';
		  
	    fly.config.timeout=10000;
		fly.config.parseJson=true;
		fly.interceptors.response.use(
			    (response) => {
			        //只将请求结果的data字段返回
			        return response.data
			    },
			    (err) => {
			        //发生网络错误后会走到这里
			        //return Promise.resolve("ssss")
			    }
		)
	    let ret = await fly.get(url).catch(() => false);
		let playerdata = (ret === false?null:JSON.parse(ret));
		console.log("getPlayers playerdata --:"+null!=playerdata?JSON.stringify(playerdata):null);
		this.assign('lowerpnum', null!=playerdata?playerdata.data.arr.length:0);
	    
	    this.meta_title = '代理信息';
	    // 判断浏览客户端
	    if (this.isMobile) {
	      if (this.isAjax('get')) {
	        return this.json(data);
	      } else {
	        this.active = 'center/agent/index';
	        return this.display(this.mtpl());
	      }
	    } else {
	      return this.display();
	    }
  }
  
//代理信息
  async grantAction() {
		//const id = this.para('id') || null;
	    
		const userInfo = await this.model('member').find(this.user.uid);
		this.assign('proxy_id', userInfo.proxy_id);
		const group = await this.model('member_group').where({groupid:Number(userInfo.groupid)+1}).find();
		this.assign('nlname',think.isEmpty(group)?sectionToChinese(Number(userInfo.groupid)+1)+"级代理":group.name);
	    this.meta_title = '代理授权';
	    // 判断浏览客户端
	    if (this.isMobile) {
	      if (this.isAjax('get')) {
	        return this.json(data);
	      } else {
	        this.active = 'center/agent/index';
	        return this.display(this.mtpl());
	      }
	    } else {
	      return this.display();
	    }
  }
  
};
